import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Rectangle {
    id: mountainPage
    color: "#2d5a27" // 绿色背景

    // 顶部控制栏
    Rectangle {
        id: controlBar
        width: parent.width
        height: 80
        color: "#3d6a37"
        
        RowLayout {
            anchors.fill: parent
            anchors.margins: 15
            spacing: 15

            // 添加新记录按钮
            Button {
                Layout.preferredHeight: 50
                font.pixelSize: 16
                font.bold: true
                
                background: Rectangle {
                    color: parent.pressed ? "#4CAF50" : "#66BB6A"
                    radius: 25
                }
                
                contentItem: Text {
                    text: "🏔️ 添加登山记录"
                    color: "white"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    font: parent.font
                }
                
                onClicked: {
                    addRecordDialog.open()
                }
            }

            // 统计信息
            Rectangle {
                Layout.fillWidth: true
                height: 50
                color: "#4a7c59"
                radius: 25
                
                Row {
                    anchors.centerIn: parent
                    spacing: 20
                    
                    Text {
                        text: "总记录: " + mountainRecordModel.totalRecords
                        color: "white"
                        font.pixelSize: 14
                        font.bold: true
                    }
                    
                    Text {
                        text: "总距离: " + mountainRecordModel.totalDistance.toFixed(1) + "km"
                        color: "white"
                        font.pixelSize: 14
                        font.bold: true
                    }
                    
                    Text {
                        text: "总时长: " + Math.floor(mountainRecordModel.totalDuration / 60) + "h" + (mountainRecordModel.totalDuration % 60) + "m"
                        color: "white"
                        font.pixelSize: 14
                        font.bold: true
                    }
                }
            }
        }
    }

    // 记录列表视图
    ScrollView {
        anchors.top: controlBar.bottom
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        
        ListView {
            id: recordListView
            model: mountainRecordModel
            spacing: 10
            
            delegate: Rectangle {
                width: recordListView.width - 20
                height: 120
                x: 10  // 使用x定位替代anchors
                color: "#4a7c59"
                radius: 10
                border.color: "white"
                border.width: 2
                
                Row {
                    anchors.fill: parent
                    anchors.margins: 15
                    spacing: 15
                    
                    // 山图标
                    Rectangle {
                        width: 80
                        height: 80
                        radius: 40
                        color: getDifficultyColor(model.difficulty)
                        anchors.verticalCenter: parent.verticalCenter
                        
                        Text {
                            text: "🏔️"
                            font.pixelSize: 30
                            anchors.centerIn: parent
                        }
                    }
                    
                    // 记录信息
                    Column {
                        anchors.verticalCenter: parent.verticalCenter
                        spacing: 5
                        
                        Text {
                            text: model.mountainName
                            color: "white"
                            font.pixelSize: 18
                            font.bold: true
                        }
                        
                        Text {
                            text: model.location
                            color: "#CCCCCC"
                            font.pixelSize: 14
                        }
                        
                        Text {
                            text: "海拔: " + model.altitude + "m | 距离: " + model.distance + "km | 时长: " + Math.floor(model.duration / 60) + "h" + (model.duration % 60) + "m"
                            color: "#CCCCCC"
                            font.pixelSize: 12
                        }
                        
                        Text {
                            text: "难度: " + getDifficultyText(model.difficulty) + " | 天气: " + model.weather
                            color: "#CCCCCC"
                            font.pixelSize: 12
                        }
                    }
                }
                
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        recordDetailDialog.recordId = model.id
                        recordDetailDialog.open()
                    }
                }
            }
        }
    }

    // 添加记录对话框
    Dialog {
        id: addRecordDialog
        title: "添加登山记录"
        width: 500
        height: 600
        
        Rectangle {
            color: "#2a2a2a"
            anchors.fill: parent
            
            ScrollView {
                anchors.fill: parent
                anchors.margins: 20
                
                Column {
                    width: parent.width
                    spacing: 15
                    
                    Text {
                        text: "🏔️ 添加新登山记录"
                        color: "white"
                        font.pixelSize: 20
                        font.bold: true
                        anchors.horizontalCenter: parent.horizontalCenter
                    }
                    
                    // 山名输入
                    TextField {
                        id: mountainNameField
                        width: parent.width
                        placeholderText: "山名"
                        color: "white"
                        background: Rectangle {
                            color: "#3a3a3a"
                            radius: 5
                        }
                    }
                    
                    // 地点输入
                    TextField {
                        id: locationField
                        width: parent.width
                        placeholderText: "地点"
                        color: "white"
                        background: Rectangle {
                            color: "#3a3a3a"
                            radius: 5
                        }
                    }
                    
                    // 坐标输入
                    Row {
                        width: parent.width
                        spacing: 10
                        
                        TextField {
                            id: latitudeField
                            width: parent.width / 2 - 5
                            placeholderText: "纬度"
                            color: "white"
                            background: Rectangle {
                                color: "#3a3a3a"
                                radius: 5
                            }
                        }
                        
                        TextField {
                            id: longitudeField
                            width: parent.width / 2 - 5
                            placeholderText: "经度"
                            color: "white"
                            background: Rectangle {
                                color: "#3a3a3a"
                                radius: 5
                            }
                        }
                    }
                    
                    // 海拔高度
                    TextField {
                        id: altitudeField
                        width: parent.width
                        placeholderText: "海拔高度 (米)"
                        color: "white"
                        background: Rectangle {
                            color: "#3a3a3a"
                            radius: 5
                        }
                    }
                    
                    // 登山距离
                    TextField {
                        id: distanceField
                        width: parent.width
                        placeholderText: "登山距离 (公里)"
                        color: "white"
                        background: Rectangle {
                            color: "#3a3a3a"
                            radius: 5
                        }
                    }
                    
                    // 登山时长
                    TextField {
                        id: durationField
                        width: parent.width
                        placeholderText: "登山时长 (分钟)"
                        color: "white"
                        background: Rectangle {
                            color: "#3a3a3a"
                            radius: 5
                        }
                    }
                    
                    // 天气
                    TextField {
                        id: weatherField
                        width: parent.width
                        placeholderText: "天气"
                        color: "white"
                        background: Rectangle {
                            color: "#3a3a3a"
                            radius: 5
                        }
                    }
                    
                    // 难度等级
                    Row {
                        width: parent.width
                        spacing: 10
                        
                        Text {
                            text: "难度等级:"
                            color: "white"
                            font.pixelSize: 16
                            anchors.verticalCenter: parent.verticalCenter
                        }
                        
                        Slider {
                            id: difficultySlider
                            width: parent.width - 100
                            from: 1
                            to: 5
                            value: 3
                            stepSize: 1
                        }
                        
                        Text {
                            text: Math.round(difficultySlider.value)
                            color: "white"
                            font.pixelSize: 16
                            anchors.verticalCenter: parent.verticalCenter
                        }
                    }
                    
                    // 描述
                    TextArea {
                        id: descriptionField
                        width: parent.width
                        height: 100
                        placeholderText: "登山描述..."
                        color: "white"
                        background: Rectangle {
                            color: "#3a3a3a"
                            radius: 5
                        }
                    }
                    
                    // 按钮
                    Row {
                        width: parent.width
                        spacing: 10
                        
                        Button {
                            text: "取消"
                            width: parent.width / 2 - 5
                            onClicked: addRecordDialog.close()
                        }
                        
                        Button {
                            text: "添加"
                            width: parent.width / 2 - 5
                            onClicked: {
                                mountainRecordModel.addRecord(
                                    mountainNameField.text,
                                    locationField.text,
                                    parseFloat(latitudeField.text) || 0,
                                    parseFloat(longitudeField.text) || 0,
                                    QStringList(), // 照片列表
                                    descriptionField.text,
                                    parseFloat(altitudeField.text) || 0,
                                    parseFloat(distanceField.text) || 0,
                                    parseInt(durationField.text) || 0,
                                    weatherField.text,
                                    Math.round(difficultySlider.value)
                                )
                                addRecordDialog.close()
                            }
                        }
                    }
                }
            }
        }
    }

    // 记录详情对话框
    Dialog {
        id: recordDetailDialog
        property int recordId: 0
        
        title: "登山记录详情"
        width: 400
        height: 500
        
        Rectangle {
            color: "#2a2a2a"
            anchors.fill: parent
            
            ScrollView {
                anchors.fill: parent
                anchors.margins: 20
                
                Column {
                    width: parent.width
                    spacing: 10
                    
                Text {
                    text: {
                        if (recordDetailDialog.recordId >= 0) {
                            var record = mountainRecordModel.getRecordById(recordDetailDialog.recordId)
                            return (record && record.mountainName) ? record.mountainName : ""
                        }
                        return ""
                    }
                    color: "white"
                    font.pixelSize: 24
                    font.bold: true
                    anchors.horizontalCenter: parent.horizontalCenter
                }
                    
                    Text {
                        text: "地点: " + mountainRecordModel.getRecordById(recordDetailDialog.recordId).location
                        color: "white"
                        font.pixelSize: 16
                    }
                    
                    Text {
                        text: "海拔: " + mountainRecordModel.getRecordById(recordDetailDialog.recordId).altitude + " 米"
                        color: "white"
                        font.pixelSize: 16
                    }
                    
                    Text {
                        text: "距离: " + mountainRecordModel.getRecordById(recordDetailDialog.recordId).distance + " 公里"
                        color: "white"
                        font.pixelSize: 16
                    }
                    
                    Text {
                        text: "时长: " + Math.floor(mountainRecordModel.getRecordById(recordDetailDialog.recordId).duration / 60) + "h" + (mountainRecordModel.getRecordById(recordDetailDialog.recordId).duration % 60) + "m"
                        color: "white"
                        font.pixelSize: 16
                    }
                    
                    Text {
                        text: "天气: " + mountainRecordModel.getRecordById(recordDetailDialog.recordId).weather
                        color: "white"
                        font.pixelSize: 16
                    }
                    
                    Text {
                        text: "难度: " + getDifficultyText(mountainRecordModel.getRecordById(recordDetailDialog.recordId).difficulty)
                        color: "white"
                        font.pixelSize: 16
                    }
                    
                    Text {
                        text: "描述: " + mountainRecordModel.getRecordById(recordDetailDialog.recordId).description
                        color: "white"
                        font.pixelSize: 16
                        wrapMode: Text.Wrap
                        width: parent.width
                    }
                    
                    Button {
                        text: "关闭"
                        anchors.horizontalCenter: parent.horizontalCenter
                        onClicked: recordDetailDialog.close()
                    }
                }
            }
        }
    }

    // 根据难度返回颜色
    function getDifficultyColor(difficulty) {
        switch(difficulty) {
            case 1: return "#4CAF50" // 绿色
            case 2: return "#8BC34A" // 浅绿色
            case 3: return "#FFC107" // 黄色
            case 4: return "#FF9800" // 橙色
            case 5: return "#F44336" // 红色
            default: return "#4CAF50"
        }
    }

    // 根据难度返回文本
    function getDifficultyText(difficulty) {
        switch(difficulty) {
            case 1: return "简单"
            case 2: return "容易"
            case 3: return "中等"
            case 4: return "困难"
            case 5: return "极难"
            default: return "简单"
        }
    }
}
